unit UnThreadVareIPs;

interface

uses
  Classes {$IFDEF MSWINDOWS}, Windows {$ENDIF};

type
  TThreadVareIPs = class(TThread)
  private
    procedure SetName;
  protected
    procedure Execute; override;
  end;

implementation

uses UnPrincipal, SysUtils, Forms, HostTest;

{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TVareIPs.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}

{ TVareIPs }

procedure TThreadVareIPs.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'VareIPs';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

procedure TThreadVareIPs.Execute;
var a, b, c, d: Byte;
    IP: string[15];
begin
SetName;
with Form1 do begin
  BtParar.Enabled := True;
  for a := IPInicio.AddressValues.Value1 to IPFim.AddressValues.Value1 do
    for b := IPInicio.AddressValues.Value2 to IPFim.AddressValues.Value2 do
      for c := IPInicio.AddressValues.Value3 to IPFim.AddressValues.Value3 do
        for d := IPInicio.AddressValues.Value4 to IPFim.AddressValues.Value4 do begin
          IP := Format('%d.%d.%d.%d', [a, b, c, d]);
          Barra.Panels[0].Text := 'Ping ' + IP;
          Application.ProcessMessages;
          if TestTCPPort(IP, StrToIntDef(Port.Text,80),StrToIntDef(TimeOut.Text, 100)) then Memo.Lines.Add(IP + ':' + Port.EditText);
          end;
  BtParar.Enabled := False;
  end;
end;

end.
